package class03;

import java.util.HashMap;
import java.util.TreeMap;

/**
 * hash表查询数据, 时间复杂度为O(1)
 * 但是常数时间比较大, 远大于数组直接寻址
 */
public class Code05_HashMapTreeMap {

  public static class Node {
    public int value;

    public Node(int v) {
      value = v;
    }
  }

  // (K V)表
  public static void main(String[] args) {
    System.out.println("----------------hash表,O(1),查基本操作----------------");
    HashMap<String, String> map = new HashMap<>();
    map.put("zuochengyun", "我是左程云");
    System.out.println(map.containsKey("zuochengyun"));
    System.out.println(map.containsKey("zuo"));
    System.out.println(map.get("zuochengyun"));

    map.put("zuochengyun", "他是左程云");
    System.out.println(map.get("zuochengyun"));

    // 移除
    // map.remove("zuochengyun");
    // System.out.println(map.containsKey("zuochengyun"));
    // System.out.println(map.get("zuochengyun"));

    System.out.println("----------------引用传递/值传递,原生类型,hash表查的是值----------------");
    String test1 = "zuochengyun";
    String test2 = "zuochengyun";
    System.out.println(map.containsKey(test1));
    System.out.println(map.containsKey(test2));

    HashMap<Integer, String> map2 = new HashMap<>();
    map2.put(1234567, "我是1234567");

    Integer a = 1234567;
    Integer b = 1234567;

    System.out.println(a == b);
    System.out.println(map2.containsKey(a));
    System.out.println(map2.containsKey(b));

    System.out.println("----------------引用传递/值传递,引用类型,hash表查的是地址值----------------");
    Node node1 = new Node(1);
    Node node2 = new Node(1);
    HashMap<Node, String> map3 = new HashMap<>();
    map3.put(node1, "我进来了！");
    System.out.println(map3.containsKey(node1));
    System.out.println(map3.containsKey(node2));
    System.out.println("----------------所以,如果想节省map空间,那么存引用地址,实际数据根据地址获取----------------");
    System.out.println("");

    System.out.println("----------------treeMap,有序, O(logN)----------------");
    TreeMap<Integer, String> treeMap1 = new TreeMap<>();

    treeMap1.put(3, "我是3");
    treeMap1.put(0, "我是3");
    treeMap1.put(7, "我是3");
    treeMap1.put(2, "我是3");
    treeMap1.put(5, "我是3");
    treeMap1.put(9, "我是3");

    System.out.println(treeMap1.containsKey(7));
    System.out.println(treeMap1.containsKey(6));
    System.out.println(treeMap1.get(3));

    treeMap1.put(3, "他是3");
    System.out.println(treeMap1.get(3));

    treeMap1.remove(3);
    System.out.println(treeMap1.get(3));

    System.out.println("----------------treeMap,特殊用法----------------");
    System.out.println(treeMap1.firstKey());
    System.out.println(treeMap1.lastKey());
    // <=5 离5最近的key告诉我
    System.out.println(treeMap1.floorKey(5));
    // <=6 离6最近的key告诉我
    System.out.println(treeMap1.floorKey(6));
    // >=5 离5最近的key告诉我
    System.out.println(treeMap1.ceilingKey(5));
    // >=6 离6最近的key告诉我
    System.out.println(treeMap1.ceilingKey(6));

    //引用类型需要指定比较器, 否则无法使用
    //		Node node3 = new Node(3);
    //		Node node4 = new Node(4);
    //		TreeMap<Node, String> treeMap2 = new TreeMap<>();
    //		treeMap2.put(node3, "我是node3");
    //		treeMap2.put(node4, "我是node4");

  }
}
